"""
Case Type   : 兼容M*数据库maketime函数
Case Name   : maketiime函数，入参无效取值测试
Create At   : 2022.11.25
Owner       : z@wan005
Description :
    1、第一个参数hour超范围
    2、minute第二个参数测试，超范围
    3、second第三个参数测试，超范围
    4、参数含null
    5、入参为空字符串
    6、参数含特殊字符
    7、含空字符串
    8、hour为2^32-1或hour为2^32
    9、微秒导致返回值越界
    10、清理环境
Expect      :
    1、则根据hour的正负类型，来返回指定的边界值
    2、返回空值
    3、返回空值
    4、返回空
    5、弹出warning并返回结果
    6、弹出warning并输出结果
    7、弹出warning并输出结果
    8、返回time类型极值
    9、则根据hour的正负类型，来返回指定的边界值
    10、成功
History     :modified by z@wan005，2023.09.20，step5入参是空串，select场景下不报错，
            优化用例断言
            modified by zhanghuan96,2024.7.5，报错提示变更
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_maketime_0002"

    def test_maketime(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:第一个参数hour超范围;' \
               'expect:则根据hour的正负类型，来返回指定的边界值--'
        self.log.info(text)
        sql_cmd = "select maketime('839',0,0);" \
                  "select maketime('-839',0,0);" \
                  "select maketime(9223372036854775807,0,0);" \
                  "select maketime(-9223372036854775808,0,0);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 19, '行数不到19行')
        self.assertEqual('838:59:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('-838:59:59', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('00:00:00', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)
        self.assertEqual('838:59:59', sql_res.splitlines()[-7].strip(),
                         '执行失败' + text)

        text = '--step2:minute第二个参数测试，超范围;expect:返回空值--'
        self.log.info(text)
        sql_cmd = "select maketime(838,-1,59);" \
                  "select maketime(838,60,59);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到9行')
        self.assertEqual(0, len(sql_res.splitlines()[2].strip()),
                         '执行失败' + text)
        self.assertEqual(0, len(sql_res.splitlines()[-2].strip()),
                         '执行失败' + text)

        text = '--step3:second第三个参数测试，超范围;expect:返回空值--'
        self.log.info(text)
        sql_cmd = "select maketime(838,0,-1);" \
                  "select maketime(838,25,60);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到9行')
        self.assertEqual(0, len(sql_res.splitlines()[2].strip()),
                         '执行失败' + text)
        self.assertEqual(0, len(sql_res.splitlines()[-2].strip()),
                         '执行失败' + text)

        text = '--step4:参数含null;expect:返回空--'
        self.log.info(text)
        sql_cmd = "select maketime(null,null,null);" \
                  "select maketime(838,null,60);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到29行')
        self.assertEqual(0, len(sql_res.splitlines()[2].strip()),
                         '执行失败' + text)
        self.assertEqual(0, len(sql_res.splitlines()[-2].strip()),
                         '执行失败' + text)

        text = '--step5:入参为空字符串;expect:弹出warning并返回结果--'
        self.log.info(text)
        sql_cmd = "select maketime('','','');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(3, sql_res.count('WARNING'), '执行失败' + text)
        self.assertIn('00:00:00', sql_res, '执行失败' + text)

        text = '--step6:参数含特殊字符;expect:弹出warning并输出结果--'
        self.log.info(text)
        sql_cmd = "select maketime(24,'59*',59.9999991);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('WARNING:  invalid input syntax for type bigint: "59*"',
                      sql_res, '执行失败' + text)
        self.assertIn('24:59:59.999999', sql_res, '执行失败' + text)

        text = '--step7:含空字符串;expect:弹出warning并输出结果--'
        self.log.info(text)
        sql_cmd = "select maketime('','',59.9999991);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count(
            'WARNING:  invalid input syntax for type bigint: ""'),
                         '执行失败' + text)
        self.assertIn('00:00:59.999999', sql_res, '执行失败' + text)

        text = '--step8:hour为2^32-1或hour为2^32;expect:返回time类型极值--'
        self.log.info(text)
        sql_cmd = "select maketime(4294967295,0,0);" \
                  "select maketime(4294967296,0,0);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count('838:59:59'), '执行失败' + text)

        text = '--step9:微秒导致返回值越界;' \
               'expect:根据hour的正负类型，来返回指定的边界值--'
        self.log.info(text)
        sql_cmd = "select maketime(838,59,59.9999999);" \
                  "select maketime(838, 59, 59.000000500);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(2, sql_res.count('838:59:59'), '执行失败' + text)

    def tearDown(self):
        text = '----step10:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
